package cn.itcast.springMVC.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.springMVC.domain.AjaxResponse;
import cn.itcast.springMVC.domain.Post;
import cn.itcast.springMVC.mapper.PostMapper;

/**
 * 具体实现Post代码逻辑
 * @author 16604
 *
 */
@Service
@Transactional
public class PostService{
	
	@Autowired
	PostMapper mapper;
	
	//查询所有文章
	public List<Post> findByAll() {
		
		List<Post> findByAll = mapper.findByAll();
		
		return findByAll;
		
	}
	
	//根据分页查询文章并包含分类
	public AjaxResponse<Post> findPage(int start, int size) {
		
		List<Post> page = mapper.findPage((start-1)*size, size-1);
		
		int findCount = mapper.findCount();
		
		AjaxResponse<Post> ajaxResponse = new AjaxResponse<>();
		ajaxResponse.setCount(findCount);
		ajaxResponse.setData(page);
		ajaxResponse.setCode(0);
		ajaxResponse.setMsg("");
		
		return ajaxResponse;
	}
	
	//批量删除
	public void deleteBatchPost(String[] ids) {
		
		mapper.deleteBatchPost(ids);
		
	}
	
	//增加一篇文章
	public void insertPost(Post post) {
		
		mapper.insertPost(post);
		
	}
	
	//搜索文章
	public AjaxResponse<Post> searchPost(String title,String created,Integer cid,int start,int size) {
		
		List<Post> searchPost = mapper.searchPost(title,created,cid,(start-1)*size,size);
		
		int findCount = mapper.searchCount(title, created, cid);
		
		AjaxResponse<Post> ajaxResponse = new AjaxResponse<>();
		ajaxResponse.setCount(findCount);
		ajaxResponse.setData(searchPost);
		ajaxResponse.setCode(0);
		ajaxResponse.setMsg("");
		
		return ajaxResponse;
	}
	
	//根据id更新文章title
	public void updataPost(Post post) {
		
		mapper.updatePost(post);
		
	}
	
	public void run() {
		System.out.println("run...");
	}
	
}
